package io.hilo.example.biz01.web;

import org.hilo.boot.core.BaseMsg;
import org.hilo.boot.core.UT;
import org.hilo.boot.core.exception.AppException;
import org.jretty.apibase.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.hilo.example.biz01.pojo.LoginUser;

/**
 * Controller Demo
 * 
 * @author xxxx
 * @since 2023-10-12
 */
@RestController
@RequestMapping("/app")
public class DemoRestController {
    
    // 1、文本表单："content-type":"application/x-www-form-urlencoded"
    // curl http://localhost:8080/app/hello?qs=66 -X POST -d "world=zollty&type=12&type=34"
    //
    // 2、数据表单："content-type":"multipart/form-data"
    // curl http://localhost:8080/app/hello?qs=66 -X POST -d "world=zollty&type=12&type=34"
    //
    // 3、指定Content-Type
    // curl http://localhost:8080/app/hello?qs=66 -H "Content-Type: application/json" -X POST
    // --data '{"world":"world", "type": [12, 34]}'
    //
    // 4、GET
    // curl http://localhost:8080/app/hello?sss=1&sss=3&ss=23
    //
    // 5、Body：application/json
    // curl http://localhost:8080/app/hi?qs=66 -H "Content-Type: application/json" -X POST
    // --data '{"username":"world", "type": [12, 34]}'
    //
    @RequestMapping("/hello")
    public Result<?> hiResponseBody(String world, String hello) {

        // String token = StpUserUtil.getTokenValue();
        // System.err.println("=================>" + token);
        // System.err.println("=================>" + StpUserUtil.getLoginIdByToken(token));
        // String userId = String.valueOf(StpUserUtil.getLoginId());
        // System.err.println("=================>" + userId);
        // System.err.println("=================>" + StpUserUtil.getLoginDevice());
        //
        // System.err.println("=================>" + StpUserUtil.getTokenTimeout());
        // System.err.println("=================>" + StpUserUtil.getSessionTimeout());

        if (world != null && world.equalsIgnoreCase("error")) {
            throw new AppException(BaseMsg.UNDEFINED_ERR);
        }

        if (world != null && world.equalsIgnoreCase("timeout")) {
            UT.Thread.sleepThread(61000);
            throw new AppException(BaseMsg.UNDEFINED_ERR);
        }

        return Result.success("springboot framework running! hello " + world);
    }

    /**
     * hiRequestBody <br/>
     * curl -H "Content-Type: application/json" -X POST http://localhost:8080/app/hi?qs=66
     */
    @RequestMapping("/hi")
    public Result<?> hiRequestBody(@RequestBody(required = false) LoginUser test) {
        if (test != null) {
            return Result.success("springboot framework running! hello " + test.getUsername());
        }
        return Result.success("springboot framework running! hello " + null);
    }

    /**
     * hiVoid <br/>
     * curl -H "Content-Type: application/json" -X POST http://localhost.com:8080/app/void?qs=66
     */
    @RequestMapping("/void")
    public void hiVoid(@RequestBody(required = false) LoginUser test) {

    }

}
